﻿using System.IO;
using System.Windows;
using System.Xml.Serialization;

namespace KarliCards_Gui
{
  /// <summary>
  /// Interaction logic for Options.xaml
  /// </summary>
  public partial class Options : Window
  {
    private GameOptions _gameOptions;

    public Options()
    {
      
      if (_gameOptions == null)
      {
        //如果_gameOptions等于null，并且存在GameOptions.xml，反序列化文件
        if (File.Exists("GameOptions.xml"))
        {
          using (var stream = File.OpenRead("GameOptions.xml"))
          {
            var serializer = new XmlSerializer(typeof(GameOptions));
            _gameOptions = serializer.Deserialize(stream) as GameOptions;
          }
        }
        else
          _gameOptions = new GameOptions();
      }
      //加入数据上下文
      DataContext = _gameOptions;
      //System.Console.WriteLine(_gameOptions.ComputerSkill);
      InitializeComponent();
      //初始化单选按钮状态
      switch (_gameOptions.ComputerSkill)
      {
        case ComputerSkillLevel.Dumb:
            dumbAIRadioButton.IsChecked = true;
            break;
        case ComputerSkillLevel.Good:
            goodAIRadioButton.IsChecked = true;
            break;
        case ComputerSkillLevel.Cheats:
            cheatingAIRadioButton.IsChecked = true;
            break;

      }
    }

    private void dumbAIRadioButton_Checked(object sender, RoutedEventArgs e)
    {
      _gameOptions.ComputerSkill = ComputerSkillLevel.Dumb;
    }

    private void goodAIRadioButton_Checked(object sender, RoutedEventArgs e)
    {
      _gameOptions.ComputerSkill = ComputerSkillLevel.Good;
    }

    private void cheatingAIRadioButton_Checked(object sender, RoutedEventArgs e)
    {
      _gameOptions.ComputerSkill = ComputerSkillLevel.Cheats;
    }

    private void okButton_Click(object sender, RoutedEventArgs e)
    {
            using (var stream = File.Open("GameOptions.xml", FileMode.Create))
            {
                var serializer = new XmlSerializer(typeof(GameOptions));
                serializer.Serialize(stream, _gameOptions);
            }
            Close();
    }

    private void cancelButton_Click(object sender, RoutedEventArgs e)
    {
      _gameOptions = null;
      Close();
    }
  }
}
